import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		tableData: [{
			id: Math.random(),
			name: '王小虎',
			address: '上海市普陀区金沙江路 1518 弄',
			age: 18,
			isAlone: 1,
			account: 0,
			iphone: 18711115555,
			idCard: 43068196606066666,
			inoculation: 0
		}]
	},
	getters: {
		isAloneCount(state) {
			return state.tableData.reduce((a, b) => b.isAlone ? ++a : a, 0)
		},
		accountCount(state) {
			return state.tableData.reduce((a, b) => b.account ? ++a : a, 0)
		},
		inoculationCount(state) {
			return state.tableData.reduce((a, b) => b.inoculation ? ++a : a, 0)
		},
		ageCount(state) {
			return state.tableData.reduce((a, b) => b.age >= 18 ? ++a : a, 0)
		}
	},
	mutations: {
		addTableData(s,obj){
			obj.id = Math.random()
			s.tableData.push(obj)
		},
		updateTableDate(s,obj){
			s.tableData = s.tableData.map(item=>item.id===obj.id?obj:item)
		},
		deleteTableData(s,arr){
			s.tableData = s.tableData.filter(item=> !arr.includes(item.id))
		}
	},
	plugins: [createPersistedState()]
})
